#include "Enemy.h"

Enemy::Enemy()
	: mActive(false) //why is this better?
	, mZigZagTimer (0)
{
}

Enemy::~Enemy()
{
}

void Enemy::Load()
{
	mSprite.Load("burger.png");
}

void Enemy::Unload()
{
	mSprite.Unload();
}

void Enemy::Update(float deltaTime)
{
	//only update active Enemys
	if(mActive)
	{
		//*****ZIG-ZAG PATTERN*****
		mZigZagTimer -= deltaTime;

		if(mZigZagTimer <= 0.0f)
		{
			mVelocity.x *= -1;
			mZigZagTimer = RandomFloat(0.5f, 2.0f);
		}

		//access window dimensions through sge.ini file
		//provide a default when calling a value that may not exist (800/600)
		const int kWinWidth = IniFile_GetInt("WinWidth", 800);
		const int kWinHeight = IniFile_GetInt("WinHeight", 600);

		const int kThreshold = mSprite.GetWidth();

		mPosition += mVelocity * deltaTime;

		if(mPosition.x < -kThreshold || mPosition.x > kWinWidth + kThreshold||
			mPosition.y < -kThreshold || mPosition.y > kWinHeight + kThreshold)
		{
			Kill(); //kill Enemy if it exceeds the boundary of the window
		}

	}
}

void Enemy::Render()
{
	//only render active Enemys
	if(mActive)
	{
		int width = mSprite.GetWidth();
		int height = mSprite.GetHeight();

		SVector2 offset(width * 0.5f, height * 0.5f);
		SVector2 renderPosition = mPosition - offset;

		mSprite.SetPosition(renderPosition);
		mSprite.Render();
	}
}

void Enemy::Spawn(const SVector2& pos)
{
	mPosition = pos;
	mVelocity = SVector2((RandomFloat(-200.0f, 200.0f)), 200.0f); //enemy moves right and down (-y)
	mActive = true;
}

void Enemy::Kill()
{
	//set false to stop update and render functions
	mActive = false;
}